今天要來紀錄常用的“Mutating Joins” 函式,用來將兩個資料表根據匹配的列進行合併
建立範例資料表
library(dplyr)
# 建立表格 x
x <- tribble(
~A, ~B, ~C,
"a", "t", 1,
"b", "u", 2,
"c", "v", 3
)
# 建立表格 y
y <- tribble(
~A, ~B, ~D,
"a", "t", 3,
"b", "u", 2,
"d", "w", 1
)
# 左連接
left_join_result <- left_join(x, y, by = c("A", "B"))
# 查看結果
print(left_join_result)
# 右連接
right_join_result <- right_join(x, y, by = c("A", "B"))
# 查看結果
print(right_join_result)
# 內連接
inner_join_result <- inner_join(x, y, by = c("A", "B"))
# 查看結果
print(inner_join_result)
# 全連接
full_join_result <- full_join(x, y, by = c("A", "B"))
# 查看結果
print(full_join_result)
參考資料: